﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Data.SqlClient;
using RED_NET_Core.BLL;
using RED_NET_Core.Entity;
using RED_NET_Core.Utilty;
namespace RED_NET.ContentPage
{
    public partial class ShowDaily : System.Web.UI.Page
    {
        private static Users user;
        public static int idTo;
        public static int visitId;
        public static int _Id;
        protected void Init()
        {
            Session["PageName"] = "ShowDaily";
            user = new Users();
            user.UserId = -1;
            if (Request.QueryString["_IsMessage"] != null)
            {

                Label_Title.Text = "留言板";
            }
          

            if (Session["USER"] != null)
                user = (Users)Session["USER"];
            if (Request.QueryString["_Id"] != null && Request.QueryString["VISIT_ID"] != null)
            {
                _Id = Int32.Parse(Request.QueryString["_Id"]);

                visitId = Int32.Parse(Request.QueryString["VISIT_ID"]);
            }
            else
            {
                Response.Redirect("~/ContentPage/Error/PageError.aspx");
            }
            if (user.UserId== -1)
            {

                CommentArea.Visible = false;
                LinkButton_Edit.Visible = false;
                LinkButton_Delete.Visible = false;
            }
            else if (visitId == user.UserId && Request.QueryString["_IsMessage"] == null)
            {
                LinkButton_Edit.Visible = true;
                LinkButton_Delete.Visible = true;
            }
            if (user.Right == 3)
                LinkButton_Delete.Visible = true;
            News _news = new News();

            try
            {
                _news = News_BLL.ShowNews(_Id);
            }
            catch (Exception ex)
            {
                Response.Redirect("~/ContentPage/Error/PageError.aspx");
            }
          
            LoginBLL _login = new LoginBLL();
            Users _user = _login.GetMessageOfUser(_news.UId);
            Label_Read.Text = "阅读(" + _news.ReadNum.ToString() + ")";
            Label_Comment.Text = "评论（" + _news.ReplyNum.ToString() + "）";
            idTo = _news.Id;
            Label_Content.Text = _news.Content;
            if (Request.QueryString["_IsMessage"] != null)
            {

                Label_Title.Text = "留言板";
                if (_news.Level != 4)
                    Response.Redirect("~/ContentPage/Error/PageError.aspx");
            }
            else
            {
                if (_news.Level != 2)
                    Response.Redirect("~/ContentPage/Error/PageError.aspx");
                Label_Title.Text = _news.Title;
                if (_news.UId != user.UserId&&(!IsPostBack))
                {

                    try
                    {
                        News_BLL.AddDailyReadNum(_news.Id);
                    }
                    catch (Exception ex)
                    {
                         Response.Redirect("~/ContentPage/Error/PageError.aspx");
                    }
                }

            }
            Label_Author.Text = _user.NickName;
            Label_UpdateTime.Text = _news.UpdateTime.ToString();
            try
            {
                DataTable _dt = News_BLL.ShowCommentList(_Id);

                DataList_Comment.DataSource = _dt;
                DataList_Comment.DataKeyField = "_Id";
                DataList_Comment.DataBind();
            }
            catch (Exception ex)
            {
                Response.Redirect("~/ContentPage/Error/PageError.aspx");
            }


        }

        protected void LinkButton_Edit_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/ContentPage/News/PublicDaily.aspx?_IsEdit=1&VISIT_ID="+visitId.ToString()+"&_NewsId=" + idTo.ToString());
        }
        protected void LinkButton_Delete_Click(object sender, EventArgs e)
        {
            try
            {
                News_BLL.DeleteNews(_Id);
            }
            catch (Exception ex)
            {
                Response.Redirect("~/ContentPage/Error/PageError.aspx");
            }
            News _news = News_BLL.ShowNews(_Id);
            if (user.Right == 3 && _news.UId != user.UserId)
            {

                string title = "日志删除通知"; 
                string _content = "很抱歉，您的日志:\"" + Label_Title.Text + "\"由于涉及违规内容已被删除！";
                if (Request.QueryString["_IsMessage"] != null)
                {
                    title = "留言删除通知";
                    _content = "很抱歉，您在\"" + user.NickName + "\"博客的留言由于涉及违规内容已被删除！";
                }
               
                int _num = 1;
                int[] _sendto = new int[10];
                _sendto[0] = visitId;
                LoginBLL _login = new LoginBLL();
                Users _user = _login.GetMessageOfUser(_news.UId);
                string _sendtoStr = _user.NickName + ";";
                News_BLL.AddLetter(user.UserId, _content, title, _sendto, _num, _sendtoStr);
            }
            Response.Redirect("DailyList.aspx?VISIT_ID=" + visitId.ToString());
        }
        protected void Page_Load(object sender, EventArgs e)
        {
           
            Init();
        }

        protected void Button_AddComment_Click(object sender, EventArgs e)
        {

            string _content = TextBox_Comment.Text;
            _content = this.Server.HtmlEncode(_content).Replace("\n", " <br/>").Replace(" ", " ");//用HTML换行符替代Textbox中的换行符
            _content = this.Server.HtmlDecode(_content);
            ReturnValue ret = Safety.JudgeStr(_content, 5);
            if (ret.value == 0)
            {
                try{
                News_BLL.AddComment(user.UserId, idTo, _content);
                }
                catch (Exception ex)
                {
                    Response.Redirect("~/ContentPage/Error/PageError.aspx");
                }
                //  Response.Redirect("~/ContentPage/News/ShowDaily.aspx?_IsMessage=1&_Id=" + idTo.ToString() + "&VISIT_ID=" + visitId.ToString());
                Init();
                Label_Comment_Error.Text = "";
                TextBox_Comment.Text = "";
            }
            else
            {
                Label_Comment_Error.Text = ret.error;
            }


        }
        protected void DataList_Comment_DeleteCommand(object source, DataListCommandEventArgs e)
        {
            int _id = (int)DataList_Comment.DataKeys[e.Item.ItemIndex];
            try
            {
                News_BLL.DeleteComment(_id);
            }
            catch (Exception ex)
            {
                Response.Redirect("~/ContentPage/Error/PageError.aspx");
            }
            Comment _comment = new Comment();
            
            if (user.Right == 3&&_comment.UId!=user.UserId)
            {

                string title = "评论删除通知";
                string _content = "很抱歉，您对\""+user.NickName+"\"日志:\"" + Label_Title.Text + "\"的评论由于涉及违规内容已被删除！";
                if (Request.QueryString["_IsMessage"] != null)
                {
                    title = "留言删除通知";
                    _content = "很抱歉，您在\"" + user.NickName + "\"博客的留言由于涉及违规内容已被删除！";
                }
                int _num = 1;
                int[] _sendto = new int[10];
                _sendto[0] = visitId;
                LoginBLL _login = new LoginBLL();
                Users _user = _login.GetMessageOfUser(_comment.UId);
                string _sendtoStr = _user.NickName + ";";
                News_BLL.AddLetter(user.UserId, _content, title, _sendto, _num, _sendtoStr);
            }
            Init();
        }

        protected void DataList_ItemCreated(Object sender, DataListItemEventArgs e)
        {
            if (user.UserId != visitId&&user.Right!=3)
            {
                LinkButton LinkButton_Delete = (LinkButton)e.Item.FindControl("LinkButton_Delete");
                LinkButton_Delete.Visible = false;
            }
            else
            {
                LinkButton LinkButton_Delete = (LinkButton)e.Item.FindControl("LinkButton_Delete");
                LinkButton_Delete.Visible = true;

            }


        }
    }
}
